package com.jumi.microservice.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jumi.microservice.constant.ExceptionEnum;
import com.jumi.microservice.domain.ImageSpaceDO;
import com.jumi.microservice.domain.PhotoAlbumDO;
import com.jumi.microservice.domain.PhotoAlbumDTO;
import com.jumi.microservice.mapper.ImageSpaceMapper;
import com.jumi.microservice.mapper.PhotoAlbumMapper;
import com.jumi.microservice.service.PhotoAlbumService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 商城相册表 服务实现类
 * </p>
 *
 * @author liuz
 * @since 2020-07-27
 */
@Service
@Transactional
public class PhotoAlbumServiceImpl extends ServiceImpl<PhotoAlbumMapper, PhotoAlbumDO> implements PhotoAlbumService {

    @Autowired
    private ImageSpaceMapper imageSpaceMapper;

    /**
     * 添加相册
     * @param dto
     */
    @Override
    public void insert(PhotoAlbumDTO dto) {
        PhotoAlbumDO po = dto2Po(dto);
        ExceptionEnum.FAIL_ALBLUM_NULL.doThrowIf(po==null);
        LocalDateTime now = LocalDateTime.now();
        po.setCreateTime(now);
        po.setUpdateTime(now);
        baseMapper.insert(po);
    }

    /**
     * 根据相册id统计数量
     * @return
     */
    @Override
    public List<ImageSpaceDO> selectCountByaAlnumID(List<Long> pid) {
//        QueryWrapper<ImageSpaceDO> queryWrapper = new QueryWrapper<ImageSpaceDO>();
//        queryWrapper.select("photo_album_id photoAlbumId, image_cover, count(photo_album_id) imageSize");
//        queryWrapper.groupBy("photo_album_id");
        return CollectionUtil.isNotEmpty(pid)?imageSpaceMapper.selectList4Cover(pid):new ArrayList<>();
    }

    /**
     * dto->po
     * @param dto
     * @return
     */
    private PhotoAlbumDO dto2Po(PhotoAlbumDTO dto){
        PhotoAlbumDO po = null;
        try {
            po = dto.clone(PhotoAlbumDO.class);
        }catch (Exception e){
            ExceptionEnum.FAIL_CHANGE.newException();
        }
        return po;
    }
}
